import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.Set;

public class Util {
    private static String lexicalTxtPath = "lexical.txt";

    public static void setLexicalTxtPath(String lexicalTxtPath) {
        Util.lexicalTxtPath = lexicalTxtPath;
    }

    public static String getLexicalTxtPath() {
        return lexicalTxtPath;
    }

    public static void test() {
        System.out.println("test");
    }

    public static String toLower(String str) {
        return str.toLowerCase();
    }

    public static boolean isAllLetter(String str) {
        return str.matches("^[a-zA-Z]+$");
    }

    public static void printToken(String token, TokenCode code, int lineNum) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(lexicalTxtPath, true))) {
            String result = "";
            result += token;
            result += '\t';
            result += "<";

            // 根据 code 判断类型
            switch (code) {
                // 关键词
                case KW_INT:
                case KW_FLOAT:
                case KW_CHAR:
                case KW_VOID:
                case KW_RETURN:
                case KW_CONST:
                case KW_MAIN:
                case KW_UNION:
                case KW_STRUCT:
                case KW_SWITCH:
                case KW_DEFAULT:
                case KW_BREAK:
                case KW_CASE:
                    result += "KW";
                    break;

                    // 运算符
                case OP_NOT:
                case OP_PLUS:
                case OP_MINUS:
                case OP_MULTIPLY:
                case OP_DIVIDE:
                case OP_PERCENT:
                case OP_EQUAL:
                case OP_GT:
                case OP_LT:
                case OP_ASSIGN:
                case OP_LEQ:
                case OP_GEQ:
                case OP_NEQ:
                case OP_AND:
                case OP_OR:
                    result += "OP";
                    break;

                    // 界符
                case SE_LPARENT:
                case SE_RPARENT:
                case SE_LBRACES:
                case SE_RBARCES:
                case SE_SEMICOLON:
                case SE_COMMA:
                case SE_COLON:
                case SE_SINGLE_QUOTE:
                    result += "SE";
                    break;

                    // 标识符和整数
                case IDN:
                    result += "IDN";
                    break;
                case INT:
                    result += "INT";
                    break;
                case FLOAT:
                    result += "FLOAT";
                    break;
                case CHAR:
                    result += "CHAR";
                    break;

                    // 未定义
                case UNDEFINED:
                    result += "UNDEFINED";
            }

            if (code != TokenCode.IDN && code != TokenCode.INT) {
                result += "," + code.ordinal() + ">";
            } else {
                result += "," + token + ">";
            }

            // 将结果写入文件
            writer.write(result);
            writer.newLine();

            // 如果是未定义的 token，输出到控制台
            if (code == TokenCode.UNDEFINED) {
                System.out.println("UNDEFINED token: " + token + " in line " + lineNum);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Set<Character> getLetterList(char c) {
        Set<Character> result = new HashSet<>();

        // 判断是否为字母或下划线
        if (Character.isLetter(c) || c == '_') {
            Set<Character> letterList = new HashSet<>();
            letterList.add('_');

            // 添加小写字母 'a' 到 'z'
            for (char ch = 'a'; ch <= 'z'; ch++) {
                letterList.add(ch);
            }

            // 添加大写字母 'A' 到 'Z'
            for (char ch = 'A'; ch <= 'Z'; ch++) {
                letterList.add(ch);
            }
            return letterList;

            // 如果是字符 '0'
        } else if (c == '0') {
            result.add('0');
            return result;
        } else if (c == '.') {
            result.add('.');
            return result;
            // 如果是字符 '1' 到 '9'
        } else if (c >= '1' && c <= '9') {
            Set<Character> numList = new HashSet<>();
            for (char ch = '1'; ch <= '9'; ch++) {
                numList.add(ch);
            }
            return numList;
        }

        // 返回空集
        return result;
    }
    public static void  removeTrailingEmptyLine(){
        try {
            // 读入
            String content = Files.readString(Paths.get(lexicalTxtPath));

            // 去除末尾空行
            content = content.stripTrailing();

            // 写回
            Files.writeString(Paths.get(lexicalTxtPath), content, StandardOpenOption.TRUNCATE_EXISTING);

//            System.out.println("Trailing empty line removed successfully.");
        } catch (IOException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }
}